package cz.hanis.android.geography.activities;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cz.hanis.android.geography.R;
import cz.hanis.android.geography.TestActivity;
import cz.hanis.android.geography.R.id;
import cz.hanis.android.geography.R.layout;
import cz.hanis.android.geography.R.string;
import cz.hanis.android.geography.models.Country;


import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class ContiguousCountryTestActivity extends TestActivity {


	private Button btCheck;
	private TextView tvCountry;	
	
	private Country country;

	private List<Country> correctContiguous = new ArrayList<Country>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTestLayout(R.layout.contiguous_countries_test);
		setQuestion(getString(R.string.tv_contiguous_country_question));
		tvCountry = (TextView) findViewById(R.id.tv_contiguous_country_country);
		setQuestionView(tvCountry);
		addAnswerView((CheckBox) findViewById(R.id.cb_contiguous_country_choice1));
		addAnswerView((CheckBox) findViewById(R.id.cb_contiguous_country_choice2));
		addAnswerView((CheckBox) findViewById(R.id.cb_contiguous_country_choice3));
		addAnswerView((CheckBox) findViewById(R.id.cb_contiguous_country_choice4));
		addAnswerView((CheckBox) findViewById(R.id.cb_contiguous_country_choice5));

		btCheck = (Button) findViewById(R.id.bt_contiguous_country_check);
		btCheck.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				answer();
			}
		});			
		newRound();				
	}

	
	
	@Override
	protected int onAnswer(int answer) {		
		int result = TestActivity.ANSWERE_NONE;	
		if(answer == TestActivity.ANSWERE_TIMED_OUT) {
			result = TestActivity.ANSWERE_TIMED_OUT;
		} else {
			if(isCorrect()) {
				result = TestActivity.ANSWERE_CORRECT;
			} else {
				result = TestActivity.ANSWERE_INCORRECT;
			}
		}
		return result;
	}	
	
	private boolean isCorrect() {			
		boolean correct = true;
		for(View view : getAnswerViews()) {
			CheckBox checkBox = (CheckBox) view;
			if (correctContiguous.contains((Country) checkBox.getTag()) == checkBox.isChecked()) {
				checkBox.setTextColor(Color.GREEN);
			} else {
				checkBox.setTextColor(Color.RED);
				correct = false;
			}			
		}		
		btCheck.setEnabled(false);
		return correct;
	}

	
	private void activeteAnswer(int location, int color, Country country) {
		CheckBox answer = (CheckBox) getAnswerViews().get(location);
		answer.setTextColor(color);
		answer.setText(country.getName());
		answer.setTag(country);
		answer.setChecked(false);		
	}
	
	
	@Override
	protected void onNewRound() {
		btCheck.setEnabled(true);

	    country = getTools().getRandomCountry(getTest().getContinent());
		tvCountry.setText(country.getName());
		
		int count = getAnswerViews().size(); 
		int corrects = min(country.getContiguousCountries().size(),
				getTools().getRandom(count + 1));
		correctContiguous = getRandomContiguous(
				country.getContiguousCountries(), corrects);
		List<Country> all = getListWithIncorrectContiguous(
				correctContiguous, count);
		Collections.shuffle(all);	
		for(int i = 0; i < count; i++) {
			activeteAnswer(i, Color.WHITE, all.get(i));
		}
	}	

	private int min(int a, int b) {
		if (a < b) {
			return a;
		}
		return b;
	}

	private List<Country> getRandomContiguous(List<Country> countries, int number) {
		if (countries.size() == number) {
			return countries;
		}
		List<Integer> list = getTools().getRandomList(countries.size(), number);
		List<Country> randomContiguous = new ArrayList<Country>();
		for (int i = 0; i < number; i++) {
			randomContiguous.add(countries.get(list.get(i)));
		}
		return randomContiguous;
	}

	private List<Country> getListWithIncorrectContiguous(List<Country> corrects,
			int number) {
		List<Country> newList = new ArrayList<Country>();
		newList.addAll(corrects);
		while (newList.size() < number) {
			Country c = getTools().getRandomCountry(getTest().getContinent());
			if (!newList.contains(c) 
					&& !c.equals(country)
					&& !country.getContiguousCountries().contains(c)) {
				newList.add(c);
			}
		}
		return newList;
	}



	@Override
	protected void onRestoreRound() {
		// TODO Auto-generated method stub
		
	}

}
